package accs;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import accs.model.UserPasswordPair;
import accs.model.io.DuplicateFilter;
import accs.model.io.SimpleUserPasswordReader;
import accs.model.io.UserPasswordReader;

public class PasswordStats {
	private static class PassCounter implements Comparable<PassCounter> {
		public String pass=null;
		public int count=0;
		
		public PassCounter(String pass) {
			this.pass=pass;
			this.count=1;
		}
		
		public void inc() {
			count++;
		}

		public int compareTo(PassCounter o) {
			return new Integer(this.count).compareTo(o.count);
		}
		
		public String toString() {
			return count+" -> "+pass;
		}
	}
	
	private static List<PassCounter> data=new ArrayList<PassCounter>();
	
	private static void incPassword(String pass) {
		for(int i=0; i<data.size(); i++) {
			if(data.get(i).pass.equals(pass)) {
				data.get(i).inc();
				return;
			}
		}
		
		data.add(new PassCounter(pass));
	}
	
	public static void main(String[] args) throws FileNotFoundException, IOException {
		UserPasswordReader reader=new DuplicateFilter(new SimpleUserPasswordReader(new FileReader(new File("../Logs/UNCHECKED/log45_checked.txt"))));
		UserPasswordPair pair=null;
		
		while((pair=reader.read())!=null) {
			incPassword(pair.getPassword());
		}
		
		Collections.sort(data);
		Collections.reverse(data);
		
		BufferedWriter out=new BufferedWriter(new FileWriter("pw_stats3.txt"));
		
		for(int i=0; i<data.size(); i++) {
			if(data.get(i).count==1) break;
			out.write(data.get(i).toString());
			out.newLine();
		}
		
		out.close();
	}
}